package com.yowasa.xiuxian.engine.callback.map;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yowasa.xiuxian.annoations.NOTIFY;
import com.yowasa.xiuxian.engine.BattleEngine;
import com.yowasa.xiuxian.engine.HoneEngine;
import com.yowasa.xiuxian.engine.callback.CmdCallBackHandler;
import com.yowasa.xiuxian.engine.hone.MentalInterface;
import com.yowasa.xiuxian.engine.message.activity.MemberRoleHandler;
import com.yowasa.xiuxian.entity.bo.battle.BattleContent;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.mys.ClickMsgComponent;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.ExploreBattleLog;
import com.yowasa.xiuxian.entity.po.XiuXianExploreResource;
import com.yowasa.xiuxian.entity.po.XiuXianExploreUser;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.*;
import com.yowasa.xiuxian.util.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

@NOTIFY(tag = "抢劫")
public class ExploreBattleHandler extends CmdCallBackHandler {
    public static final Logger logger = LoggerFactory.getLogger(ExploreBattleHandler.class);

    @Autowired
    private MessageHelper messageHelper;

    @Autowired
    private ExploreHelper exploreHelper;

    @Autowired
    private UserHelper userHelper;

    @Autowired
    private BattleEngine battleEngine;

    @Autowired
    private BattleHelper battleHelper;

    @Autowired
    private ItemHelper itemHelper;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void execute(ClickMsgComponent message) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.setRoom_id(message.getRoom_id());
        sendMessage.setVilla_id(message.getVilla_id());
        String uid = message.getUid();
        sendMessage.setFrom_user_id(uid);
        JSONObject jsonObject = JSONUtil.parseObj(message.getExtra());

        Long accountId = jsonObject.getLong("accountId");

        UserBO userBO = userHelper.checkOpUserAndState(sendMessage);

        if(userBO.getId().equals(accountId)){
            throw new HandlerException("干嘛，自己打自己？");
        }
        XiuXianExploreUser exploreUser = exploreHelper.selectUser(userBO.getId());
        XiuXianExploreUser exploreBattleUser = exploreHelper.selectUser(accountId);

        exploreHelper.checkBattle(exploreUser,exploreBattleUser);
        UserBO battleUser = userHelper.getById(accountId);
        if(ObjectUtil.isNull(battleUser)){
            throw new HandlerException("该用户不存在或已死亡");
        }
        if(battleUser.getUserBattle().getChp() <= 0){
            throw new HandlerException("哥们他都没血啦，已经鼠掉了，咱换个人打吧");
        }
        try {
            ExploreBattleLog battleLog = new ExploreBattleLog();
            Integer days = RandomUtil.randomInt(1,3);
            StringBuffer sb = new StringBuffer();
            sb.append("你花费了").append(days).append("天准备埋伏他一手\n");
            BattleContent content = battleEngine.exploreBattle(userBO, battleUser,exploreUser,exploreBattleUser);
            //战斗时长
            int days1 = battleHelper.calCostTime(content);
            days += days1;
            sb.append("你与").append(battleUser.getName()).append("战斗了").append(days1).append("天\n");
            battleHelper.syncUserState(content);
            exploreHelper.lockUser(exploreUser, days, userBO.getName() + "正在战斗中");
            exploreHelper.lockUser(exploreBattleUser, days, battleUser.getName() + "正在战斗中");
            //业力 + 1
            exploreUser.setKarma(exploreUser.getKarma() + 1);
            battleLog.setAccountId(userBO.getId());
            battleLog.setBattleId(battleUser.getId());
            battleLog.setBattleResult(content.getResult());
            battleLog.setCreateTime(new Date());
            if (content.getResult() == 1) {
                //打赢了，抢东西
                Integer karma = exploreBattleUser.getKarma() > 5 ? 5 : exploreBattleUser.getKarma();
                Integer goldNum =(int) (exploreBattleUser.getGoldNum() * 0.1 * (karma + 1));
                sb.append("你抢夺了对方").append(goldNum).append("个雷之印");
                exploreUser.setGoldNum(exploreUser.getGoldNum() + goldNum);
                exploreBattleUser.setGoldNum(exploreBattleUser.getGoldNum() - goldNum);
                battleLog.setGoldNum(goldNum);
            }else if(content.getResult() == -1){
                //打输了，乐，被抢东西
                Integer karma = exploreUser.getKarma() > 5 ? 5 : exploreUser.getKarma();
                Integer goldNum =(int) (exploreUser.getGoldNum() * 0.1 * karma);
                sb.append("乐，你没打过，被对方抢走了").append(goldNum).append("个雷之印");
                exploreBattleUser.setGoldNum(exploreBattleUser.getGoldNum() + goldNum);
                exploreUser.setGoldNum(exploreUser.getGoldNum() - goldNum);
                battleLog.setGoldNum(goldNum);
            }else {
                sb.append("你们不相上下，都奈何不了对方，没有抢到雷之印");
                battleLog.setGoldNum(0);
            }
            exploreHelper.updateUser(exploreUser);
            exploreHelper.updateUser(exploreBattleUser);
            String msg = content.getMsgLog().stream().collect(Collectors.joining("\n"));
            sb.append(msg);
            exploreHelper.insertBattleLog(battleLog);
            messageHelper.replyMys(sendMessage,sb.toString());
        }catch (Exception e){
            e.printStackTrace();
            throw new HandlerException("出现了未知错误QAQ");
        }

    }



}
